package cn.kawa.service.impl;

import cn.kawa.domain.MovieTicket;
import cn.kawa.domain.UserMovie;
import cn.kawa.exception.GlobalCustomException;
import cn.kawa.mapper.MovieTicketMapper;
import cn.kawa.mapper.UserMovieMapper;
import cn.kawa.result.JsonResult;
import cn.kawa.service.IMovieTicketService;
import cn.kawa.service.IUserMovieService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-09-01
 */
@Service
public class MovieTicketServiceImpl extends ServiceImpl<MovieTicketMapper, MovieTicket> implements IMovieTicketService {

    @Autowired
    private IUserMovieService userMovieService;

    /**
     * 需求2
     * 传入要购买的电影票Id，调用电影服务进行购买，购买成功扣减用户余额
     * @param
     * @return
     */
    @Override
    public JsonResult buy(Long userId,Long movieId) {
        MovieTicket movieTicket = selectById(movieId);
        if(movieTicket.getStock()==0){
            return JsonResult.error("库存为空，请联系管理员");
        }
        EntityWrapper<UserMovie> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("movie_ticket_id",movieId);
        UserMovie userMovieBySelect = userMovieService.selectOne(wrapper);
        if(userMovieBySelect==null){
            UserMovie userMovie = new UserMovie();
            userMovie.setMovieTicketId(movieId);
            userMovie.setStock(1);
            userMovie.setUserId(userId);
            userMovieService.insert(userMovie);
        }else {
            userMovieBySelect.setStock(userMovieBySelect.getStock()+1);
            userMovieService.updateById(userMovieBySelect);
        }
        movieTicket.setStock(movieTicket.getStock()-1);
        updateById(movieTicket);
        return JsonResult.success(movieTicket);
    }

    /**
     * 需求3：新增用户时，保存用户信息，并调用电影服务，随机送一张电影票
     * @param userId
     * @return
     */
    @Override
    public JsonResult give(Long userId) {
        List<MovieTicket> tickets = selectList(null);
        MovieTicket ticket = tickets.get(new Random().nextInt(tickets.size()));
        buy(userId,ticket.getId());
        return JsonResult.success("用户创建成功，并赠送电影票"+ticket.getName());
    }
    /**
     * 需求4：传入要观看的电影Id，调用电影服务进行判断，返回提示信息
     */
    @Override
    public JsonResult watch(Long userId, Long movieId) {
        MovieTicket movieTicket = selectById(movieId);
        EntityWrapper<UserMovie> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("movie_ticket_id",movieId);
        UserMovie userMovieBySelect = userMovieService.selectOne(wrapper);
        if(userMovieBySelect==null ||userMovieBySelect.getStock()==0 ){
            return JsonResult.error("用户无此票，请购买");
        }
        if(movieTicket.getEndTime().getTime()<new Date().getTime()){
            return JsonResult.error("电影已过期，无法观看");
        }
        userMovieBySelect.setStock(userMovieBySelect.getStock()-1);
        userMovieService.updateById(userMovieBySelect);
        return JsonResult.success("可以观看，请及时入场");
    }
}
